Page({
    data: {
        payShow: false,
        payLoading: false,
        obj: null,
        textColorData: '#000000',
        list: [],
        backgroundData: 'transparent',
        // 下面为支付参数
        goods: [],
        down_from: 1, //默认立即购买来的，2------购物车
        pay_price: '', //支付金额
        pay_jf: '',
        address_id: '',
        addrName: '',
        stepNum: 1,
        totalPrice: '',
        // paytype: 1,
        isPayActive: null, //1-->微信支付方式
        zhjf: '',
        id: '',
        dk_jf: 0,
        // offPayType: null, //null-->线下支付方式
        wxPay: false,
        xxPay: false,
    },

    onLoad(options) {
        if (options) {
            console.log();
            const id = options.id;
            this.setData({
                down_from: options.ftp,
                id,
            });

            this.getPayType();
        }
        this.getJsDetail();
    },

    onShow() {
        this.getDefault();
        // this.getPersonJifen();
    },

    getPayType() {
        this.request({
            url: 'common/payConfig',
        }).then(res => {
            console.log('支付方式', res.data.wx_pay);
            this.setData({
                wxPay: res.data.wx_pay,
                xxPay: res.data.xx_pay,
            });
        });
    },

    // 获取用户积分
    // getPersonJifen() {
    //     this.request({
    //         url: 'user/money/msg',
    //     }).then(res => {
    //         console.log(res);
    //         this.setData({
    //             zhjf: res.data.jf,
    //         });
    //     });
    // },

    tabPayType(e) {
        // console.log();
        this.setData({
            isPayActive: e.currentTarget.dataset.paynum,
        });
    },

    // 获取结算详情(订单预支付)
    getJsDetail() {
        this.request({
            url: 'user/my/order/payMessage',
            data: {
                goods: [{ id: this.data.id, num: 1 }],
            },
        }).then(res => {
            // console.log(res);
            this.setData({
                list: res.data,
                stepNum: res.data.goods[0].num,
                zhjf: res.data.dk_jf,
            });
        });
    },

    // 获取默认收货地址
    getDefault() {
        this.request({
            url: 'user/my/address2/default',
        }).then(res => {
            console.log(res);
            if (res.data) {
                this.setData({
                    obj: res.data,
                    address_id: res.data.id,
                    addrName: res.data.address_code_name.join('-'),
                });
            }
        });
    },

    onVisibleChange() {
        this.setData({
            payShow: false,
        });
    },

    // 步进器
    stepChange(e) {
        console.log(e);
        this.setData({
            stepNum: e.detail,
            totalPrice: (this.data.list.pay_price * e.detail).toFixed(2),
        });
        this.request({
            url: 'user/my/order/payMessage',
            data: {
                goods: [{ id: this.data.id, num: this.data.stepNum }],
            },
        }).then(res => {
            console.log('修改后的预支付', res);
            this.setData({
                zhjf: res.data.dk_jf,
            });
        });
    },

    onClickPay() {
        if (!this.data.obj)
            return wx.showModal({
                title: '提示',
                content: '是否前往完善收货信息',
                confirmColor: '#F11224',
                complete: res => {
                    if (res.confirm) {
                        wx.navigateTo({
                            url: '/pages/address/address',
                        });
                    }
                },
            });

        const list = this.data.list;

        this.setData({
            payShow: true,
            pay_price: (list.pay_price * this.data.stepNum).toFixed(2),
            goods: [{ id: list.goods[0].id, num: this.data.stepNum }],
            address_id: this.data.obj.id,
            pay_jf: list.dk_jf * this.data.stepNum,
            down_from: 1,
        });
    },

    // 最终确认支付
    okPay() {
        if (this.data.isPayActive==null)
            return wx.showToast({
                title: '请选择支付方式',
                icon: 'none',
            });
        this.setData({
            payLoading: true,
        });

        // isPayActive==1(微信支付)
        if (this.data.isPayActive === 1) {
            this.request({
                url: 'user/my/order/pay',
                method: 'POST',
                data: {
                    goods: this.data.goods,
                    down_from: this.data.down_from,
                    pay_price: this.data.pay_price,
                    pay_jf: this.data.zhjf,
                    address_id: this.data.address_id,
                    pay_type: this.data.isPayActive,
                },
            })
                .then(async res => {
                    // console.log(res);
                    // if (res.data.pay_data == null) {
                    //     wx.showToast({
                    //         title: res.msg,
                    //     });
                    // }
                    const { timeStamp, paySign, signType, package: packageId, nonceStr } = res.data.pay_data;
                    wx.requestPayment({
                        timeStamp: String(timeStamp),
                        nonceStr,
                        package: packageId,
                        signType,
                        paySign,
                        success: async res => {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                            });
                            await this.sleep(1000);
                            wx.navigateBack();
                        },
                        fail: async err => {
                            wx.showToast({
                                title: '支付失败',
                                icon: 'none',
                            });
                            await this.sleep(1000);
                            wx.navigateBack();
                        },
                    });

                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                })
                .catch(err => {
                    console.log(err);
                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                });
        } else {
            // 线下支付
            this.request({
                url: 'user/my/order/pay',
                method: 'POST',
                data: {
                    goods: this.data.goods,
                    down_from: this.data.down_from,
                    pay_price: this.data.pay_price,
                    pay_jf: this.data.zhjf,
                    address_id: this.data.address_id,
                    pay_type: this.data.isPayActive,
                },
            })
                .then(async res => {
                    if (res.data.pay_data == null) {
                        wx.showToast({
                            title: res.msg,
                            duration: 1800,
                        });
                        this.setData({
                            payLoading: false,
                            payShow: false,
                        });
                        await this.sleep(1600);
                        wx.navigateBack();
                    }
                })
                .catch(err => {
                    this.setData({
                        payLoading: false,
                        payShow: false,
                    });
                });
        }
    },

    onPageScroll(e) {
        if (e.scrollTop > 1) {
            this.setData({
                textColorData: '#ffffff',
                backgroundData: '#FFCCCC',
            });
        } else {
            this.setData({
                textColorData: '#000000',
                backgroundData: 'transparent',
            });
        }
    },
});
